package indexingSystem;

import java.util.Vector;

public class Document {
	int id; //FIXME Should be private
	private String path;
	private IndexingDatabase db; 
	private TermFrequencyVector frequency_vector = null;
	public Document(String path) throws Exception {
		this.path = path;
		db = IndexingDatabase.getInterface();
		id = db.getDocumentID(path);
		if( id == -1) {
			WordScanner scanner = new WordScanner(path);
			id = db.addDocument(path);
			while (scanner.hasNext())
				db.addWord(id, scanner.next());
			scanner.close();
		}
	}
	public int getID(){
		return id;
	}
	public TermFrequencyVector getTFV() throws Exception {
		if(frequency_vector == null)
			throw new Exception(path+": TFV not calculated.");
		return frequency_vector;
	}
	public void calcTFV(ClusterDictionary dict) throws Exception {
		Vector<String> word_vector = dict.getVector();
		Vector<Double> vector = new Vector<Double>(word_vector.size());
		for (String word : dict.getVector()) {
			vector.add((double) db.wordDocFreq(id, word));
		}
		frequency_vector = new TermFrequencyVector(vector);
	}
}
